package com.hydl.power.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.AppService;
import com.hydl.power.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AppServiceImpl implements AppService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private OrderDeliveryMenuMapper orderDeliveryMenuMapper;

    @Autowired
    private MaintainerMapper maintainerMapper;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private PowerRoomMapper powerRoomMapper;

    @Autowired
    private PowerdataMapper powerdataMapper;


    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private PushMapper pushMapper;

    @Autowired
    private  TemperatureDeviceMapper temperatureDeviceMapper;

    @Autowired
    private PartsMapper partsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private MarketingMapper marketingMapper;
    /**
     * app首页数据
     *
     * @param token
     * @return
     */
    @Override
    public Map getHomePage(String token) throws ParseException {
        Map map = new HashMap();
        List list1 = new ArrayList();
        if (StringUtils.isNoneBlank(token)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<Date> days = dateToWeek(new Date());
            String weekStartTime = sdf.format(days.get(0));
            String weekEndTime = sdf.format(days.get(6));
            SysUser user = userMapper.getUserByToken(token);
            if(user!=null){
                if (2 != user.getStatus()) {
                    //运维人员待处理的数量
                    Integer pendingOrderNumber=null;
                    if(0 == user.getStatus()){
                        pendingOrderNumber = workOrderMapper.getPendingOrderNumberSum();
                    }else{
                        pendingOrderNumber = workOrderMapper.getPendingOrderNumber(user.getMaintainerId());
                    }

                    map.put("pendingOrderNumber", pendingOrderNumber);
                    //运维人员处理中的数量
                    Integer beingProcessedOrderNumber = null;
                    if (0 == user.getStatus()) {
                        beingProcessedOrderNumber = workOrderMapper.getBeingProcessedOrderNumberSum();
                    } else {
                        beingProcessedOrderNumber = workOrderMapper.getBeingProcessedOrderNumber(user.getMaintainerId());
                    }
                    map.put("beingProcessedOrderNumber", beingProcessedOrderNumber);

                    long timeNumber = (System.currentTimeMillis() - user.getCreateTime().getTime()) / (1000 * 3600 * 24);
                    map.put("timeNumber", timeNumber);


                    /*
                     * 系统告警报表
                     */

                    //运维人员的系统告警数量月报
                    int monthArr[] = new int[6];
                    OrderReport orderCountByMonth = workOrderMapper.getOrderCountByMonth();
                    if(orderCountByMonth==null){
                        monthArr[0]=0;
                        monthArr[1]=0;
                        monthArr[2]=0;
                        monthArr[3]=0;
                        monthArr[4]=0;
                        monthArr[5]=0;
                    }else{
                        monthArr[0]=orderCountByMonth.getCommunication();
                        monthArr[1]=orderCountByMonth.getOutOfLimit();
                        monthArr[2]=orderCountByMonth.getDeflection();
                        monthArr[3]=orderCountByMonth.getElectricQuantityReversal();
                        monthArr[4]=orderCountByMonth.getTimeReversal();
                        monthArr[5]=orderCountByMonth.getDataException();
                    }
                    //map.put("monthArr",monthArr);
                    //运维人员的系统告警数量周报
                    int weekArr[] = new int[6];
                    OrderReport orderCountByWeek = workOrderMapper.getOrderCountByWeek(weekStartTime, weekEndTime);
                    if(orderCountByWeek==null){
                        weekArr[0]=0;
                        weekArr[1]=0;
                        weekArr[2]=0;
                        weekArr[3]=0;
                        weekArr[4]=0;
                        weekArr[5]=0;
                    }else{
                        weekArr[0]=orderCountByWeek.getCommunication();
                        weekArr[1]=orderCountByWeek.getOutOfLimit();
                        weekArr[2]=orderCountByWeek.getDeflection();
                        weekArr[3]=orderCountByWeek.getElectricQuantityReversal();
                        weekArr[4]=orderCountByWeek.getTimeReversal();
                        weekArr[5]=orderCountByWeek.getDataException();
                    }
                    //map.put("weekArr",weekArr);
                    //运维人员的系统告警数量年报
                    int yearArr[] = new int[6];
                    OrderReport orderCountByYear = workOrderMapper.getOrderCountByYear();
                    if(orderCountByYear==null){
                        yearArr[0]=0;
                        yearArr[1]=0;
                        yearArr[2]=0;
                        yearArr[3]=0;
                        yearArr[4]=0;
                        yearArr[5]=0;
                    }else{
                        yearArr[0]=orderCountByYear.getCommunication();
                        yearArr[1]=orderCountByYear.getOutOfLimit();
                        yearArr[2]=orderCountByYear.getDeflection();
                        yearArr[3]=orderCountByYear.getElectricQuantityReversal();
                        yearArr[4]=orderCountByYear.getTimeReversal();
                        yearArr[5]=orderCountByYear.getDataException();
                    }
                    //map.put("yearArr",yearArr);
                    list1.add(yearArr);
                    list1.add(monthArr);
                    list1.add(weekArr);
                    map.put("list1", list1);


                    /*
                     * 故障完成度报表
                     */
                    List list2 = new ArrayList();

                    //年报
                    List yeaList = new ArrayList();
                    //运维人员处理中的工单数量年报
                    int beingProcessedYearArr[] = new int[6];
                    OrderReport oderBeingProcessedCountByYear = workOrderMapper.getOrderBeingProcessedCountByYear();
                    if(oderBeingProcessedCountByYear==null){
                        beingProcessedYearArr[0]=0;
                        beingProcessedYearArr[1]=0;
                        beingProcessedYearArr[2]=0;
                        beingProcessedYearArr[3]=0;
                        beingProcessedYearArr[4]=0;
                        beingProcessedYearArr[5]=0;
                    }else{
                        beingProcessedYearArr[0]=oderBeingProcessedCountByYear.getCommunication();
                        beingProcessedYearArr[1]=oderBeingProcessedCountByYear.getOutOfLimit();
                        beingProcessedYearArr[2]=oderBeingProcessedCountByYear.getDeflection();
                        beingProcessedYearArr[3]=oderBeingProcessedCountByYear.getElectricQuantityReversal();
                        beingProcessedYearArr[4]= oderBeingProcessedCountByYear.getTimeReversal();
                        beingProcessedYearArr[5]=oderBeingProcessedCountByYear.getDataException();
                    }
                    //运维人员已完成的工单数量年报
                    OrderReport orderGoodCountByYear = workOrderMapper.getOrderGoodCountByYear();
                    int goodYearArr[] = new int[6];
                    if(orderGoodCountByYear==null){
                        goodYearArr[0]=0;
                        goodYearArr[1]=0;
                        goodYearArr[2]=0;
                        goodYearArr[3]=0;
                        goodYearArr[4]=0;
                        goodYearArr[5]=0;
                    }else{
                        goodYearArr[0]=orderGoodCountByYear.getCommunication();
                        goodYearArr[1]=orderGoodCountByYear.getOutOfLimit();
                        goodYearArr[2]=orderGoodCountByYear.getDeflection();
                        goodYearArr[3]=orderGoodCountByYear.getElectricQuantityReversal();
                        goodYearArr[4]=orderGoodCountByYear.getTimeReversal();
                        goodYearArr[5]=orderGoodCountByYear.getDataException();
                    }
                    //运维人员的工单数量总数年报
                    int sumYearArr[] = new int[6];
                    sumYearArr[0]=beingProcessedYearArr[0]+ goodYearArr[0];
                    sumYearArr[1]=beingProcessedYearArr[1]+ goodYearArr[1];
                    sumYearArr[2]=beingProcessedYearArr[2]+ goodYearArr[2];
                    sumYearArr[3]=beingProcessedYearArr[3]+ goodYearArr[3];
                    sumYearArr[4]=beingProcessedYearArr[4]+ goodYearArr[4];
                    sumYearArr[5]=beingProcessedYearArr[5]+ goodYearArr[5];

                    yeaList.add(beingProcessedYearArr);
                    yeaList.add(goodYearArr);
                    yeaList.add(sumYearArr);

                    //月报
                    List monthList = new ArrayList();
                    //运维人员处理中的工单数量月报
                    int beingProcessedMonthArr[] = new int[6];
                    OrderReport oderBeingProcessedCountByMonth = workOrderMapper.getOrderBeingProcessedCountByMonth();
                    if(oderBeingProcessedCountByMonth==null){
                        beingProcessedMonthArr[0]=0;
                        beingProcessedMonthArr[1]=0;
                        beingProcessedMonthArr[2]=0;
                        beingProcessedMonthArr[3]=0;
                        beingProcessedMonthArr[4]=0;
                        beingProcessedMonthArr[5]=0;
                    }else{
                        beingProcessedMonthArr[0]=oderBeingProcessedCountByMonth.getCommunication();
                        beingProcessedMonthArr[1]=oderBeingProcessedCountByMonth.getOutOfLimit();
                        beingProcessedMonthArr[2]=oderBeingProcessedCountByMonth.getDeflection();
                        beingProcessedMonthArr[3]=oderBeingProcessedCountByMonth.getElectricQuantityReversal();
                        beingProcessedMonthArr[4]=oderBeingProcessedCountByMonth.getTimeReversal();
                        beingProcessedMonthArr[5]=oderBeingProcessedCountByMonth.getDataException();
                    }
                    //运维人员已完成的工单数量月报
                    OrderReport orderGoodCountByMonth = workOrderMapper.getOrderGoodCountByMonth();
                    int goodMonthArr[] = new int[6];
                    if(orderGoodCountByMonth==null){
                        goodMonthArr[0]=0;
                        goodMonthArr[1]=0;
                        goodMonthArr[2]=0;
                        goodMonthArr[3]=0;
                        goodMonthArr[4]=0;
                        goodMonthArr[5]=0;
                    }else{
                        goodMonthArr[0]=orderGoodCountByMonth.getCommunication();
                        goodMonthArr[1]=orderGoodCountByMonth.getOutOfLimit();
                        goodMonthArr[2]=orderGoodCountByMonth.getDeflection();
                        goodMonthArr[3]=orderGoodCountByMonth.getElectricQuantityReversal();
                        goodMonthArr[4]=orderGoodCountByMonth.getTimeReversal();
                        goodMonthArr[5]=orderGoodCountByMonth.getDataException();
                    }
                    //运维人员的工单数量总数月报
                    int sumMonthArr[] = {beingProcessedMonthArr[0] + goodMonthArr[0],
                            beingProcessedMonthArr[1] + goodMonthArr[1],
                            beingProcessedMonthArr[2] + goodMonthArr[2],
                            beingProcessedMonthArr[3] + goodMonthArr[3],
                            beingProcessedMonthArr[4] + goodMonthArr[4],
                            beingProcessedMonthArr[5] + goodMonthArr[5]};
                    monthList.add(beingProcessedMonthArr);
                    monthList.add(goodMonthArr);
                    monthList.add(sumMonthArr);

                    //周报
                    List weekList = new ArrayList();
                    //运维人员处理中的工单数量周报
                    int beingProcessedWeekArr[] = new int[6];
                    OrderReport oderBeingProcessedCountByWeek = workOrderMapper.getOrderBeingProcessedCountByWeek(weekStartTime, weekEndTime);
                    if(oderBeingProcessedCountByWeek==null){
                        beingProcessedWeekArr[0]=0;
                        beingProcessedWeekArr[1]=0;
                        beingProcessedWeekArr[2]=0;
                        beingProcessedWeekArr[3]=0;
                        beingProcessedWeekArr[4]=0;
                        beingProcessedWeekArr[5]=0;
                    }else{
                        beingProcessedWeekArr[0]=oderBeingProcessedCountByWeek.getCommunication();
                        beingProcessedWeekArr[1]=oderBeingProcessedCountByWeek.getOutOfLimit();
                        beingProcessedWeekArr[2]=oderBeingProcessedCountByWeek.getDeflection();
                        beingProcessedWeekArr[3]=oderBeingProcessedCountByWeek.getElectricQuantityReversal();
                        beingProcessedWeekArr[4]=oderBeingProcessedCountByWeek.getTimeReversal();
                        beingProcessedWeekArr[5]=oderBeingProcessedCountByWeek.getDataException();
                    }
                    //运维人员已完成的工单数量周报
                    int goodWeekArr[] = new int[6];
                    OrderReport orderGoodCountByWeek = workOrderMapper.getOrderGoodCountByWeek(weekStartTime, weekEndTime);
                    if(orderGoodCountByWeek==null){
                        goodWeekArr[0]=0;
                        goodWeekArr[1]=0;
                        goodWeekArr[2]=0;
                        goodWeekArr[3]=0;
                        goodWeekArr[4]=0;
                        goodWeekArr[5]=0;
                    }else{
                        goodWeekArr[0]=orderGoodCountByWeek.getCommunication();
                        goodWeekArr[1]=orderGoodCountByWeek.getOutOfLimit();
                        goodWeekArr[2]=orderGoodCountByWeek.getDeflection();
                        goodWeekArr[3]=orderGoodCountByWeek.getElectricQuantityReversal();
                        goodWeekArr[4]=orderGoodCountByWeek.getTimeReversal();
                        goodWeekArr[5]=orderGoodCountByWeek.getDataException();
                    }
                    //运维人员的工单数量总数周报
                    int sumWeekArr[] = {beingProcessedWeekArr[0] + goodWeekArr[0],
                            beingProcessedWeekArr[1] + goodWeekArr[1],
                            beingProcessedWeekArr[2] + goodWeekArr[2],
                            beingProcessedWeekArr[3] + goodWeekArr[3],
                            beingProcessedWeekArr[4] + goodWeekArr[4],
                            beingProcessedWeekArr[5] + goodWeekArr[5]
                    };
                    weekList.add(beingProcessedWeekArr);
                    weekList.add(goodWeekArr);
                    weekList.add(sumWeekArr);


                    list2.add(yeaList);
                    list2.add(monthList);
                    list2.add(weekList);
                    map.put("list2", list2);
                } else {

                    //客户待处理的数量
                    Integer pendingOrderNumber = workOrderMapper.getPendingOrderNumberByProjectId(user.getProjectId());
                    map.put("pendingOrderNumber", pendingOrderNumber);
                    //客户处理中的数量
                    Integer beingProcessedOrderNumber = workOrderMapper.getBeingProcessedOrderNumberByProjectId(user.getProjectId());
                    map.put("beingProcessedOrderNumber", beingProcessedOrderNumber);
                    //客户项目安全运行天数
                    Project project = projectMapper.queryProjectByProId(user.getProjectId().toString());
                    long timeNumber = (System.currentTimeMillis() - user.getCreateTime().getTime()) / (1000 * 3600 * 24);
                    map.put("timeNumber", timeNumber);
                    //客户的系统告警月报
                    int monthArr[] = new int[6];
                    OrderReport orderCountByMonthByProjectId = workOrderMapper.getOrderCountByMonthByProjectId(user.getProjectId());
                    if(orderCountByMonthByProjectId==null){
                        monthArr[0]=0;
                        monthArr[1]=0;
                        monthArr[2]=0;
                        monthArr[3]=0;
                        monthArr[4]=0;
                        monthArr[5]=0;
                    }else{
                        monthArr[0]=orderCountByMonthByProjectId.getCommunication();
                        monthArr[1]=orderCountByMonthByProjectId.getOutOfLimit();
                        monthArr[2]=orderCountByMonthByProjectId.getDeflection();
                        monthArr[3]=orderCountByMonthByProjectId.getElectricQuantityReversal();
                        monthArr[4]=orderCountByMonthByProjectId.getTimeReversal();
                        monthArr[5]=orderCountByMonthByProjectId.getDataException();
                    }
                    //map.put("monthArr",monthArr);
                    //客户的系统告警周报
                    int weekArr[] = new int[6];
                    OrderReport orderCountByWeekByProjectId = workOrderMapper.getOrderCountByWeekByProjectId(weekStartTime, weekEndTime, user.getProjectId());
                    if(orderCountByWeekByProjectId==null){
                        weekArr[0]=0;
                        weekArr[1]=0;
                        weekArr[2]=0;
                        weekArr[3]=0;
                        weekArr[4]=0;
                        weekArr[5]=0;
                    }else{
                        weekArr[0]=orderCountByWeekByProjectId.getCommunication();
                        weekArr[1]=orderCountByWeekByProjectId.getOutOfLimit();
                        weekArr[2]=orderCountByWeekByProjectId.getDeflection();
                        weekArr[3]=orderCountByWeekByProjectId.getElectricQuantityReversal();
                        weekArr[4]=orderCountByWeekByProjectId.getTimeReversal();
                        weekArr[5]=orderCountByWeekByProjectId.getDataException();
                    }
                    //map.put("weekArr",weekArr);
                    //客户的系统告警年报
                    int yearArr[] = new int[6];
                    OrderReport orderCountByYearByProjectId = workOrderMapper.getOrderCountByYearByProjectId(user.getProjectId());
                    if(orderCountByYearByProjectId==null){
                        yearArr[0]=0;
                        yearArr[1]=0;
                        yearArr[2]=0;
                        yearArr[3]=0;
                        yearArr[4]=0;
                        yearArr[5]=0;
                    }else{
                        yearArr[0]=orderCountByYearByProjectId.getCommunication();
                        yearArr[1]=orderCountByYearByProjectId.getOutOfLimit();
                        yearArr[2]=orderCountByYearByProjectId.getDeflection();
                        yearArr[3]=orderCountByYearByProjectId.getElectricQuantityReversal();
                        yearArr[4]=orderCountByYearByProjectId.getTimeReversal();
                        yearArr[5]=orderCountByYearByProjectId.getDataException();
                    }
                    //map.put("yearArr",yearArr);
                    list1.add(yearArr);
                    list1.add(monthArr);
                    list1.add(weekArr);
                    map.put("list1", list1);

                    /*
                     * 故障完成度报表
                     */
                    List list2 = new ArrayList();
                    //年报
                    List yeaList = new ArrayList();
                    //客户处理中的工单数量年报
                    int beingProcessedYearArr[] = new int[6];
                    OrderReport oderBeingProcessedCountByYear = workOrderMapper.getOrderBeingProcessedCountYearByProjectId(user.getProjectId());
                    if(oderBeingProcessedCountByYear==null){
                        beingProcessedYearArr[0]=0;
                        beingProcessedYearArr[1]=0;
                        beingProcessedYearArr[2]=0;
                        beingProcessedYearArr[3]=0;
                        beingProcessedYearArr[4]=0;
                        beingProcessedYearArr[5]=0;
                    }else{
                        beingProcessedYearArr[0]=oderBeingProcessedCountByYear.getCommunication();
                        beingProcessedYearArr[1]=oderBeingProcessedCountByYear.getOutOfLimit();
                        beingProcessedYearArr[2]=oderBeingProcessedCountByYear.getDeflection();
                        beingProcessedYearArr[3]=oderBeingProcessedCountByYear.getElectricQuantityReversal();
                        beingProcessedYearArr[4]=oderBeingProcessedCountByYear.getTimeReversal();
                        beingProcessedYearArr[5]=oderBeingProcessedCountByYear.getDataException();
                    }
                    //客户已完成的工单数量年报
                    OrderReport orderGoodCountByYear = workOrderMapper.getOrderGoodCountYearByProjectId(user.getProjectId());
                    int goodYearArr[] = new int[6];
                    if(orderGoodCountByYear==null){
                        goodYearArr[0]=0;
                        goodYearArr[1]=0;
                        goodYearArr[2]=0;
                        goodYearArr[3]=0;
                        goodYearArr[4]=0;
                        goodYearArr[5]=0;
                    }else{
                        goodYearArr[0]=orderGoodCountByYear.getCommunication();
                        goodYearArr[1]=orderGoodCountByYear.getOutOfLimit();
                        goodYearArr[2]=orderGoodCountByYear.getDeflection();
                        goodYearArr[3]=orderGoodCountByYear.getElectricQuantityReversal();
                        goodYearArr[4]=orderGoodCountByYear.getTimeReversal();
                        goodYearArr[5]=orderGoodCountByYear.getDataException();
                    }
                    //客户的工单数量总数年报
                    int sumYearArr[] = {beingProcessedYearArr[0] + goodYearArr[0],
                            beingProcessedYearArr[1] + goodYearArr[1],
                            beingProcessedYearArr[2] + goodYearArr[2],
                            beingProcessedYearArr[3] + goodYearArr[3],
                            beingProcessedYearArr[4] + goodYearArr[4],
                            beingProcessedYearArr[5] + goodYearArr[5]
                    };
                    yeaList.add(beingProcessedYearArr);
                    yeaList.add(goodYearArr);
                    yeaList.add(sumYearArr);

                    //月报
                    List monthList = new ArrayList();

                    //客户处理中的工单数量月报
                    int beingProcessedMonthArr[] = new int[6];
                    OrderReport oderBeingProcessedCountByMonth = workOrderMapper.getOrderBeingProcessedCountMonthByProjectId(user.getProjectId());
                    if(oderBeingProcessedCountByMonth==null){
                        beingProcessedMonthArr[0]=0;
                        beingProcessedMonthArr[1]=0;
                        beingProcessedMonthArr[2]=0;
                        beingProcessedMonthArr[3]=0;
                        beingProcessedMonthArr[4]=0;
                        beingProcessedMonthArr[5]=0;
                    }else{
                        beingProcessedMonthArr[0]=oderBeingProcessedCountByMonth.getCommunication();
                        beingProcessedMonthArr[1]=oderBeingProcessedCountByMonth.getOutOfLimit();
                        beingProcessedMonthArr[2]=oderBeingProcessedCountByMonth.getDeflection();
                        beingProcessedMonthArr[3]=oderBeingProcessedCountByMonth.getElectricQuantityReversal();
                        beingProcessedMonthArr[4]=oderBeingProcessedCountByMonth.getTimeReversal();
                        beingProcessedMonthArr[5]=oderBeingProcessedCountByMonth.getDataException();
                    }
                    //客户已完成的工单数量月报
                    int goodMonthArr[] = new int[6];
                    OrderReport orderGoodCountByMonth = workOrderMapper.getOrderGoodCountMonthByProjectId(user.getProjectId());
                    if(orderGoodCountByMonth==null){
                        goodMonthArr[0]=0;
                        goodMonthArr[1]=0;
                        goodMonthArr[2]=0;
                        goodMonthArr[3]=0;
                        goodMonthArr[4]=0;
                        goodMonthArr[5]=0;
                    }else{
                        goodMonthArr[0]=orderGoodCountByMonth.getCommunication();
                        goodMonthArr[1]=orderGoodCountByMonth.getOutOfLimit();
                        goodMonthArr[2]=orderGoodCountByMonth.getDeflection();
                        goodMonthArr[3]=orderGoodCountByMonth.getElectricQuantityReversal();
                        goodMonthArr[4]=orderGoodCountByMonth.getTimeReversal();
                        goodMonthArr[5]=orderGoodCountByMonth.getDataException();
                    }
                    //客户的工单数量总数月报
                    int sumMonthArr[] = {beingProcessedMonthArr[0] +  goodMonthArr[0],
                            beingProcessedMonthArr[1] +  goodMonthArr[1],
                            beingProcessedMonthArr[2] +  goodMonthArr[2],
                            beingProcessedMonthArr[3] +  goodMonthArr[3],
                            beingProcessedMonthArr[4] +  goodMonthArr[4],
                            beingProcessedMonthArr[5] +  goodMonthArr[5]
                        };
                    monthList.add(beingProcessedMonthArr);
                    monthList.add(goodMonthArr);
                    monthList.add(sumMonthArr);

                    //周报
                    List weekList = new ArrayList();
                    //客户处理中的工单数量周报
                    int beingProcessedWeekArr[] = new int[6];
                    OrderReport oderBeingProcessedCountByWeek = workOrderMapper.getOrderBeingProcessedCountWeekByProjectId(weekStartTime, weekEndTime, user.getProjectId());
                    if(oderBeingProcessedCountByWeek==null){
                        beingProcessedWeekArr[0]=0;
                        beingProcessedWeekArr[1]=0;
                        beingProcessedWeekArr[2]=0;
                        beingProcessedWeekArr[3]=0;
                        beingProcessedWeekArr[4]=0;
                        beingProcessedWeekArr[5]=0;
                    }else{
                        beingProcessedWeekArr[0]=oderBeingProcessedCountByWeek.getCommunication();
                        beingProcessedWeekArr[1]=oderBeingProcessedCountByWeek.getOutOfLimit();
                        beingProcessedWeekArr[2]=oderBeingProcessedCountByWeek.getDeflection();
                        beingProcessedWeekArr[3]=oderBeingProcessedCountByWeek.getElectricQuantityReversal();
                        beingProcessedWeekArr[4]=oderBeingProcessedCountByWeek.getTimeReversal();
                        beingProcessedWeekArr[5]=oderBeingProcessedCountByWeek.getDataException();
                    }
                    //客户已完成的工单数量周报
                    int goodWeekArr[] = new int[6];
                    OrderReport orderGoodCountByWeek = workOrderMapper.getOrderGoodCountWeekByProjectId(weekStartTime, weekEndTime, user.getProjectId());
                    if(orderGoodCountByWeek==null){
                        goodWeekArr[0]=0;
                        goodWeekArr[1]=0;
                        goodWeekArr[2]=0;
                        goodWeekArr[3]=0;
                        goodWeekArr[4]=0;
                        goodWeekArr[5]=0;
                    }else{
                        goodWeekArr[0]=orderGoodCountByWeek.getCommunication();
                        goodWeekArr[1]=orderGoodCountByWeek.getOutOfLimit();
                        goodWeekArr[2]=orderGoodCountByWeek.getDeflection();
                        goodWeekArr[3]=orderGoodCountByWeek.getElectricQuantityReversal();
                        goodWeekArr[4]=orderGoodCountByWeek.getTimeReversal();
                        goodWeekArr[5]=orderGoodCountByWeek.getDataException();
                    }
                    //客户的工单数量总数周报
                    int sumWeekArr[] = {beingProcessedWeekArr[0] + goodWeekArr[0],
                            beingProcessedWeekArr[1] + goodWeekArr[1],
                            beingProcessedWeekArr[2] + goodWeekArr[2],
                            beingProcessedWeekArr[3] + goodWeekArr[3],
                            beingProcessedWeekArr[4] + goodWeekArr[4],
                            beingProcessedWeekArr[5] + goodWeekArr[5]
                        };
                    weekList.add(beingProcessedWeekArr);
                    weekList.add(goodWeekArr);
                    weekList.add(sumWeekArr);

                    list2.add(yeaList);
                    list2.add(monthList);
                    list2.add(weekList);
                    map.put("list2", list2);
                }
            }
            }

        return map;
    }

    /**
     * 待处理工单列表
     *
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<WorkOrder> getPendingOrder(String token, Integer pageNum, Integer pageSize) {
        List<WorkOrder> orderDeliveryList = null;
        if (StringUtils.isNoneBlank(token)) {
            SysUser user = userMapper.getUserByToken(token);
            if (2 != user.getStatus()) {
                if(user.getStatus()==0){
                    orderDeliveryList = workOrderMapper.getPendingOrder(pageNum, pageSize);
                }else{
                    orderDeliveryList = workOrderMapper.getPendingOrderByMaintainerId(pageNum, pageSize,user.getMaintainerId());
                }
            } else {
                orderDeliveryList = workOrderMapper.getPendingOrderByProjectId(pageNum, pageSize, user.getProjectId());
            }
        }
        return new PageInfo<>(orderDeliveryList);
    }

    /**
     * 处理中工单列表
     *
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<WorkOrder> getBeingProcessedOrder(String token, Integer pageNum, Integer pageSize) {
        List<WorkOrder> orderDeliveryList = null;
        if (StringUtils.isNoneBlank(token)) {
            SysUser user = userMapper.getUserByToken(token);
            if (0 == user.getStatus()) {
                orderDeliveryList = workOrderMapper.getBeingProcessedOrder(pageNum, pageSize);
            } else if (1 == user.getStatus()) {
                orderDeliveryList = workOrderMapper.getBeingProcessedOrderByMaintainerId(pageNum, pageSize, user.getMaintainerId());
            } else {
                orderDeliveryList = workOrderMapper.getBeingProcessedOrderByProjectId(pageNum, pageSize, user.getProjectId());
            }
        }
        return new PageInfo<>(orderDeliveryList);
    }

    /**
     * 已完成工单列表
     *
     * @param token
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<WorkOrder> getGoodOrder(String token, Integer pageNum, Integer pageSize) {
        List<WorkOrder> orderDeliveryList = null;
        if (StringUtils.isNoneBlank(token)) {
            SysUser user = userMapper.getUserByToken(token);
            if (0 == user.getStatus()) {
                orderDeliveryList = workOrderMapper.getGoodOrder(pageNum, pageSize);
            } else if (1 == user.getStatus()) {
                orderDeliveryList = workOrderMapper.getGoodOrderByMaintainerId(pageNum, pageSize, user.getMaintainerId());
            } else {
                orderDeliveryList = workOrderMapper.getGoodOrderByProjectId(pageNum, pageSize, user.getProjectId());
            }
        }
        return new PageInfo<>(orderDeliveryList);
    }

    /**
     * 忽略和完成工单
     * @param id
     * @param orderStatus
     * @param dispose
     * @param illustrate
     * @param reason
     * @return
     */
    @Override
    public int updateOrderStatusById(Integer id, Integer orderStatus, String dispose, String illustrate, String reason) {
        Date date = new Date();
        int i = workOrderMapper.updateOrderStatusById(orderStatus, dispose, illustrate, reason, date, id);
        orderDeliveryMapper.updateOrderStatusById(orderStatus, null, null,null, date, id);
        return  i;
    }

    /**
     * 事件记录
     * @param pageNum
     * @param pageSize
     * @param timeStatus
     * @param faultTypes
     * @return
     */
    @Override
    public PageInfo<WorkOrder> getIncidentRecord(Integer pageNum, Integer pageSize, String token,Integer timeStatus, Integer faultTypes) {
        List<WorkOrder>  list=null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String startTime="";
        String endTime="";
        if (StringUtils.isNotBlank(token)){
            SysUser user = userMapper.getUserByToken(token);
            if (user!=null){
                if (user.getStatus()!=2){
                    if(timeStatus==1){
                        startTime = simpleDateFormat.format(new Date());
                        c.add(Calendar.DAY_OF_MONTH, 1);
                        endTime = simpleDateFormat.format(c.getTime());
                        list= workOrderMapper.getIncidentRecordByFaultTypes(pageNum,pageSize,startTime,endTime,faultTypes);
                    }else if(timeStatus==2){
                        c.add(Calendar.DAY_OF_MONTH, -1);
                        startTime = simpleDateFormat.format(c.getTime());
                        endTime = simpleDateFormat.format(new Date());
                        list= workOrderMapper.getIncidentRecordByFaultTypes(pageNum,pageSize,startTime,endTime,faultTypes);
                    }else if(timeStatus==3){
                        List<Date> dates = dateToWeek(new Date());
                        startTime = simpleDateFormat.format(dates.get(0));
                        endTime = simpleDateFormat.format(dates.get(6));
                        list= workOrderMapper.getIncidentRecordByFaultTypes(pageNum,pageSize,startTime,endTime,faultTypes);
                    }else{
                        startTime = new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
                        endTime = new SimpleDateFormat("yyyy").format(new Date())+"-12-31";
                        list= workOrderMapper.getIncidentRecordByFaultTypes(pageNum,pageSize,startTime,endTime,faultTypes);
                    }
                }else {
                    if(timeStatus==1){
                        startTime = simpleDateFormat.format(new Date());
                        c.add(Calendar.DAY_OF_MONTH, 1);
                        endTime = simpleDateFormat.format(c.getTime());
                        list= workOrderMapper.getIncidentRecordByFaultTypesProjectId(pageNum,pageSize,startTime,endTime,faultTypes,user.getProjectId());
                    }else if(timeStatus==2){
                        c.add(Calendar.DAY_OF_MONTH, -1);
                        startTime = simpleDateFormat.format(c.getTime());
                        endTime = simpleDateFormat.format(new Date());
                        list= workOrderMapper.getIncidentRecordByFaultTypesProjectId(pageNum,pageSize,startTime,endTime,faultTypes,user.getProjectId());
                    }else if(timeStatus==3){
                        List<Date> dates = dateToWeek(new Date());
                        startTime = simpleDateFormat.format(dates.get(0));
                        endTime = simpleDateFormat.format(dates.get(6));
                        list= workOrderMapper.getIncidentRecordByFaultTypesProjectId(pageNum,pageSize,startTime,endTime,faultTypes,user.getProjectId());
                    }else{
                        startTime = new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
                        endTime = new SimpleDateFormat("yyyy").format(new Date())+"-12-31";
                        list= workOrderMapper.getIncidentRecordByFaultTypesProjectId(pageNum,pageSize,startTime,endTime,faultTypes,user.getProjectId());
                    }
                }
            }
        }
        return  new PageInfo<>(list);
    }


    @Override
    public int addOrderDelivery(String deviceId, Integer rank, Integer faultTypes, String fault) {
        Device device= deviceMapper.getDeviceByDeviceId(deviceId);
      List<Marketing> marketings = marketingMapper.queryMarketingByprojectId(device.getProjectId());
      if(device!=null){
            OrderDelivery orderDelivery=new OrderDelivery();
            orderDelivery.setOrderDeliveryId(IdUtil.getRandomString(8));
            orderDelivery.setOrderTypes(1);
            orderDelivery.setRank(rank);
            orderDelivery.setFault(fault);
            orderDelivery.setFaultTime(new Date());
            orderDelivery.setFaultTypes(faultTypes);
            orderDelivery.setOrderStatus(1);
            orderDelivery.setFlag(1);
            orderDelivery.setAreaId(device.getAreaId());
            orderDelivery.setAreaName(device.getAreaName());
            orderDelivery.setProjectId(device.getProjectId());
            orderDelivery.setProjectName(device.getNavId());
            orderDelivery.setRoomId(device.getPowerRoomId());
            orderDelivery.setRoomName(device.getPowerRoomName());
            orderDelivery.setDeviceId(device.getDeviceId());
            orderDelivery.setDeviceName(device.getDeviceName());
            orderDelivery.setDeviceAddress(device.getDeviceAddress());
            orderDelivery.setSn(device.getSerialNumber());
            int insert = orderDeliveryMapper.insert(orderDelivery);

            //创建工单
            WorkOrder workOrder=new WorkOrder();
            workOrder.setOrderDeliveryId(orderDelivery.getOrderDeliveryId());
            workOrder.setOrderTypes(1);
            workOrder.setRank(1);
            workOrder.setFault(fault);
            workOrder.setFaultTime(new Date());
            workOrder.setFaultTypes(faultTypes);
            workOrder.setOrderStatus(1);
            workOrder.setFlag(1);
            workOrder.setAreaId(device.getAreaId());
            workOrder.setAreaName(device.getAreaName());
            workOrder.setProjectId(device.getProjectId());
            workOrder.setProjectName(device.getNavId());
            workOrder.setRoomId(device.getPowerRoomId());
            workOrder.setRoomName(device.getPowerRoomName());
            workOrder.setDeviceId(device.getDeviceId());
            workOrder.setDeviceName(device.getDeviceName());
            workOrder.setDeviceAddress(device.getDeviceAddress());
            workOrder.setSn(device.getSerialNumber());
            workOrder.setAffirmStatus(2);
            int id=workOrderMapper.addWorkOrder(workOrder);
            //推送
            List<String> clientIdList = pushMapper.getClientId(orderDelivery.getProjectId());
            PushUtils.sendMessage(clientIdList,orderDelivery.getRoomName(),orderDelivery.getDeviceName(),orderDelivery.getFault());
            //自动派单
            WorkOrder  workOrderByProjectId = workOrderMapper.getMaintainerIdWorkOrderByProjectId(device.getProjectId());

        for (Marketing marketing : marketings) {
          if(marketing.getStatus() == 1){
            Maintainer maintainer = maintainerMapper.selectById1(marketing.getMaintainerId());
            workOrder.setMaintainerId(Integer.parseInt(maintainer.getMaintainerId()));
            workOrder.setMaintainerName(maintainer.getName());
            workOrder.setPhone(maintainer.getPhone());
            workOrder.setDealBeginTime(new Date());
            workOrder.setOrderDeliveryTime(new Date());
            workOrder.setOrderStatus(2);
            workOrderMapper.updateWorkOrderById(workOrder);
          }
          //发送短信
          Map<String, String> map = new HashMap<String, String>();
          map.put("projectName", workOrder.getProjectName());
          map.put("powerRoomName", workOrder.getRoomName());
          map.put("deviceName", workOrder.getDeviceName());
          map.put("fault", workOrder.getFault());
          String data = JSONObject.toJSONString(map);
          SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
        }
        orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
/*            if (workOrderByProjectId==null){
                Map<Double, String> distanceMap = new HashMap<>();
                List<Double> distanceList = new ArrayList<>();
                Project project = projectMapper.queryProjectByProId(device.getProjectId());
                if (WebSocketServiceImpl.locationMap.size() > 0) {
                    for (Map.Entry<String, String> entry : WebSocketServiceImpl.locationMap.entrySet()) {
                        Double distance = CalculateDistanceUtils.getDistance(entry.getValue(), project.getLongitudeLatitude() + "," + project.getLatitude_latitude(), Ellipsoid.Sphere);
                        distanceMap.put(distance, entry.getKey());
                        distanceList.add(distance);
                    }
                    Collections.sort(distanceList);
                    for (Double distance : distanceList) {
                        String maintainerId = distanceMap.get(distance);
                        List<WorkOrder> orderList = workOrderMapper.getBeingProcessedOrderByMaintainerId(null, null, maintainerId);
                        if (orderList.size() > 0) {
                            if (orderDelivery.getProjectId().equals(orderList.get(0).getProjectId())) {
                                workOrder.setMaintainerId(orderList.get(0).getMaintainerId());
                                workOrder.setMaintainerName(orderList.get(0).getMaintainerName());
                                workOrder.setPhone(orderList.get(0).getPhone());
                                workOrder.setDealBeginTime(new Date());
                                workOrder.setOrderDeliveryTime(new Date());
                                workOrder.setOrderStatus(2);
                              for (Marketing marketing : marketings) {
                                //发送短信
                                Map<String, String> map = new HashMap<String, String>();
                                map.put("projectName", workOrder.getProjectName());
                                map.put("powerRoomName", workOrder.getRoomName());
                                map.put("deviceName", workOrder.getDeviceName());
                                map.put("fault", workOrder.getFault());
                                String data = JSONObject.toJSONString(map);
                                SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                              }
                                workOrderMapper.updateWorkOrderById(workOrder);
                                orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                                break;
                            }
                        } else {
                            Maintainer maintainer = maintainerMapper.selectById1(maintainerId);
                            //创建工单
                            workOrder.setMaintainerId(Integer.valueOf(maintainer.getMaintainerId()));
                            workOrder.setMaintainerName(maintainer.getName());
                            workOrder.setPhone(maintainer.getPhone());
                            workOrder.setDealBeginTime(new Date());
                            workOrder.setOrderDeliveryTime(new Date());
                            workOrder.setOrderStatus(2);
                          for (Marketing marketing : marketings) {
                            //发送短信
                            Map<String, String> map = new HashMap<String, String>();
                            map.put("projectName", workOrder.getProjectName());
                            map.put("powerRoomName", workOrder.getRoomName());
                            map.put("deviceName", workOrder.getDeviceName());
                            map.put("fault", workOrder.getFault());
                            String data = JSONObject.toJSONString(map);
                            SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                          }
                            workOrderMapper.updateWorkOrderById(workOrder);
                            orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                            break;
                        }
                    }
                }else{
                    List<Maintainer> MaintainerList = maintainerMapper.getAll();
                    int flag=0;
                    for (Maintainer maintainer:MaintainerList){
                        if(maintainer.getMaintainerId()!=null){
                            WorkOrder workOrderByMaintainerId = workOrderMapper.getWorkOrderByMaintainerId(maintainer.getMaintainerId());
                            if(workOrderByMaintainerId==null){
                                workOrder.setMaintainerId(Integer.valueOf(maintainer.getMaintainerId()));
                                workOrder.setMaintainerName(maintainer.getName());
                                workOrder.setPhone(maintainer.getPhone());
                                workOrder.setDealBeginTime(new Date());
                                workOrder.setOrderDeliveryTime(new Date());
                                workOrder.setOrderStatus(2);
                                workOrderMapper.updateWorkOrderById(workOrder);
                                orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                              for (Marketing marketing : marketings) {
                                //发送短信
                                Map<String, String> map = new HashMap<String, String>();
                                map.put("projectName", workOrder.getProjectName());
                                map.put("powerRoomName", workOrder.getRoomName());
                                map.put("deviceName", workOrder.getDeviceName());
                                map.put("fault", workOrder.getFault());
                                String data = JSONObject.toJSONString(map);
                                SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                              }
                                flag=1;
                                break;
                            }
                        }
                    }
                    if (flag==0){
                        workOrder.setMaintainerId(94770617);
                        workOrder.setMaintainerName("张志光");
                        workOrder.setPhone("15713878160");
                        workOrder.setDealBeginTime(new Date());
                        workOrder.setOrderDeliveryTime(new Date());
                        workOrder.setOrderStatus(2);
                        workOrderMapper.updateWorkOrderById(workOrder);
                        orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                      for (Marketing marketing : marketings) {
                        //发送短信
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("projectName", workOrder.getProjectName());
                        map.put("powerRoomName", workOrder.getRoomName());
                        map.put("deviceName", workOrder.getDeviceName());
                        map.put("fault", workOrder.getFault());
                        String data = JSONObject.toJSONString(map);
                        SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                      }
                    }
                }
            }else{
                //创建工单
                workOrder.setMaintainerId(workOrderByProjectId.getMaintainerId());
                workOrder.setMaintainerName(workOrderByProjectId.getMaintainerName());
                workOrder.setPhone(workOrderByProjectId.getPhone());
                workOrder.setDealBeginTime(new Date());
                workOrder.setOrderDeliveryTime(new Date());
                workOrder.setOrderStatus(2);
              for (Marketing marketing : marketings) {
                //发送短信
                Map<String, String> map = new HashMap<String, String>();
                map.put("projectName", workOrder.getProjectName());
                map.put("powerRoomName", workOrder.getRoomName());
                map.put("deviceName", workOrder.getDeviceName());
                map.put("fault", workOrder.getFault());
                String data = JSONObject.toJSONString(map);
                SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
              }
                workOrderMapper.updateWorkOrderById(workOrder);
                orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));

            }*/

            return insert;
        }

        return 0;
    }

    /**
     * 获取城市
     * @param token
     * @return
     */
    @Override
    public List<City> getCity(String token) {
        if(StringUtils.isNotBlank(token)){
            SysUser user = userMapper.getUserByToken(token);
            if(user!=null){
                if(user.getStatus()==2){
                    if(user.getProjectId()!=null){
                        Project project = projectMapper.queryProjectByProId(user.getProjectId().toString());
                        if(project!=null){
                            City city= cityMapper.getCityByCityId(project.getCityId());
                            List<City> list=new ArrayList<>();
                            list.add(city);
                            return list;
                        }
                    }
                }else{
                    List<City> cities = cityMapper.queryCity();
                    return cities;
                }
            }
        }
        return null;
    }

    /**
     * 根据城市id获取项目
     * @param token
     * @param cityId
     * @return
     */
    @Override
    public List<Project> getProjectByCityId(String token, String cityId) {
        if(StringUtils.isNotBlank(token) && StringUtils.isNotBlank(cityId)){
            SysUser user = userMapper.getUserByToken(token);
            if(user!=null){
                if(user.getStatus()==2){
                    if(user.getProjectId()!=null){
                        List<Project> projects = new ArrayList<>();
                        Project project = projectMapper.queryProjectByProId(user.getProjectId().toString());
                        projects.add(project);
                        return projects;
                    }
                }else{
                    List<Project> projects = projectMapper.queryProjectId(cityId);
                    return projects;
                }
            }
        }
        return null;
    }

    /**
     * 根据项目id获取配电房
     * @param token
     * @param projectId
     * @return
     */
    @Override
    public List<PowerRoom> getPowerRoomByProjectId(String token, String projectId) {
        if(StringUtils.isNotBlank(token) && StringUtils.isNotBlank(projectId)){
            SysUser user = userMapper.getUserByToken(token);
            if(user!=null){
                if(user.getStatus()==2){
                    if(user.getProjectId()!=null){
                        List<PowerRoom> list= powerRoomMapper.getPowerRoomByProjectId(projectId);
                        return list;
                    }
                }else{
                    List<PowerRoom> list= powerRoomMapper.getPowerRoomByProjectId(projectId);
                    return list;
                }
            }
        }
        return null;
    }

    /**
     * 根据配电房id获取设备
     * @param token
     * @param powerRoomId
     * @return
     */
    @Override
    public List<Device> getDeviceByPowerRoomId(String token, String powerRoomId) {
        if(StringUtils.isNotBlank(token) && StringUtils.isNotBlank(powerRoomId)){
            SysUser user = userMapper.getUserByToken(token);
            if(user!=null){
                if(user.getStatus()==2){
                    if(user.getProjectId()!=null){
                        List<Device> list= deviceMapper.getDeviceByPowerRoomId(powerRoomId);
                        return list;
                    }
                }else{
                    List<Device> list= deviceMapper.getDeviceByPowerRoomId(powerRoomId);
                    return list;
                }
            }
        }
        return null;
    }

    @Override
    public Powerdata getPowerDataByDeviceId(String navId) {
        if(StringUtils.isNotBlank(navId)){
            String sn = deviceMapper.selectSnBydeviceId(navId);
            Powerdata powerdata = powerdataMapper.getPowerDataByDeviceId(sn);
            return powerdata;
        }
        return null;
    }

    @Override
    public int affirmOrderStatusById(Integer id) {
       int i= workOrderMapper.affirmOrderStatusById(id);
        return i;
    }

    /**
     * 获取测温设备
     * @param token 用户token
     * @return list
     */
    @Override
    public List<TemperatureDevice> getTemperatureDeviceListByToken(String token) {
        if(StringUtils.isNotBlank(token)){
            SysUser user = userMapper.getUserByToken(token);
            if (user!=null){
                if (user.getStatus()==2){
                    List<TemperatureDevice> list= temperatureDeviceMapper.getTemperatureDeviceListByProjectId(user.getProjectId().toString());
                    return list;
                }else {
                    List<TemperatureDevice> list=temperatureDeviceMapper.getTemperatureDeviceList();
                    return list;
                }
            }
        }
        return null;
    }


    @Override
    public List<Parts> getParts() {
        List<Parts> list= partsMapper.getParts();
        return list;
    }

    @Override
    public List<OrderDeliveryMenu> getDevice(String token) {
        SysUser user = userMapper.getUserByToken(token);
        if(user.getStatus()==2){
            if(user.getProjectId()!=null){
                OrderDeliveryMenu orderDeliveryMenuByNavId = orderDeliveryMenuMapper.getOrderDeliveryMenuByNavId(user.getProjectId().toString());
                if(orderDeliveryMenuByNavId==null){
                    return null;
                }
            }else{
                return null;
            }
        }
        List<OrderDeliveryMenu> list = orderDeliveryMenuMapper.getAll();
        ArrayList<OrderDeliveryMenu> orderDeliveryMenuList = new ArrayList<>();
        for (OrderDeliveryMenu orderDeliveryMenu: list) {
            if(orderDeliveryMenu.getpName() == null ){
                orderDeliveryMenuList.add(orderDeliveryMenu);
            }
        }
        for (OrderDeliveryMenu orderDeliveryMenu: orderDeliveryMenuList) {
            orderDeliveryMenu.setChilds(getChild(orderDeliveryMenu.getNavId(),list) );
        }
        if(user.getStatus()==2){
            for(int i=0;i<orderDeliveryMenuList.size();i++){
                List<OrderDeliveryMenu> childs = orderDeliveryMenuList.get(i).getChilds();
                for(int j=0;j< childs.size();j++){
                    if(!childs.get(j).getNavId().equals(user.getProjectId().toString())){
                        childs.get(j).setChilds(null);
                        childs.remove(j);
                    }
                    if(orderDeliveryMenuList.get(i).getChilds().size()<=0){
                        orderDeliveryMenuList.remove(i);
                    }
                }
            }
        }
        return orderDeliveryMenuList;
    }

    private ArrayList<OrderDeliveryMenu> getChild(String pid, List<OrderDeliveryMenu> orderDeliveryMenuList){
        ArrayList<OrderDeliveryMenu> childs = new ArrayList<>();
        for (OrderDeliveryMenu orderDeliveryMenu: orderDeliveryMenuList) {
            if(orderDeliveryMenu.getpName() != null){
                if(orderDeliveryMenu.getpId().equals(pid)){
                    childs.add(orderDeliveryMenu);
                }
            }
        }
        for (OrderDeliveryMenu orderDeliveryMenu: childs) {
            orderDeliveryMenu.setChilds(getChild(orderDeliveryMenu.getNavId(), orderDeliveryMenuList));
        }
        if( childs.size() == 0 ){
            return null;
        }
        return childs;
    }

    /**
     * 根据日期获得所在周的日期
     *
     * @param mdate
     * @return
     */
    public static List<Date> dateToWeek(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(fdate);
        }
        return list;
    }

  @Override
  public int addUserOrder(String  token,String fault,String deviceName,Integer rank,Integer faultTypes,String roomName,String roomId,String imageUrl) {
      SysUser sysUser=sysUserMapper.getUserByToken(token);
    if(sysUser!=null){
      Project projects=projectMapper.queryProjectByProId(sysUser.getProjectId().toString());
      if(projects!=null){
        List<Marketing> marketings = marketingMapper.queryMarketingByprojectId(projects.getProjectId());
        City city=cityMapper.getCityByCityId(projects.getCityId());
        if(city!=null){
          OrderDelivery orderDelivery=new OrderDelivery();
          orderDelivery.setOrderDeliveryId(IdUtil.getRandomString(8));
          orderDelivery.setOrderTypes(1);
          orderDelivery.setRank(rank);
          orderDelivery.setFault(fault);
          orderDelivery.setFaultTime(new Date());
          orderDelivery.setFaultTypes(faultTypes);
          orderDelivery.setOrderStatus(1);
          orderDelivery.setFlag(1);
          orderDelivery.setAreaId(projects.getCityId());
          orderDelivery.setAreaName(city.getCityName());
          orderDelivery.setProjectId(projects.getProjectId());
          orderDelivery.setProjectName(projects.getProjectName());
          orderDelivery.setRoomId(roomId);
          orderDelivery.setRoomName(roomName);
          orderDelivery.setDeviceId(null);
          orderDelivery.setDeviceName(deviceName);
          orderDelivery.setDeviceAddress(null);
          orderDelivery.setSn(null);
          orderDelivery.setImageUrl(imageUrl);
          int insert = orderDeliveryMapper.insert(orderDelivery);

          //创建工单
          WorkOrder workOrder=new WorkOrder();
          workOrder.setOrderDeliveryId(orderDelivery.getOrderDeliveryId());
          workOrder.setOrderTypes(1);
          workOrder.setRank(1);
          workOrder.setFault(fault);
          workOrder.setFaultTime(new Date());
          workOrder.setFaultTypes(faultTypes);
          workOrder.setOrderStatus(1);
          workOrder.setFlag(1);
          workOrder.setAreaId(projects.getCityId());
          workOrder.setAreaName(city.getCityName());
          workOrder.setProjectId(projects.getProjectId());
          workOrder.setProjectName(projects.getProjectName());
          workOrder.setRoomId(roomId);
          workOrder.setRoomName(roomName);
          workOrder.setDeviceId(null);
          workOrder.setDeviceName(deviceName);
          workOrder.setDeviceAddress(null);
          workOrder.setSn(null);
          workOrder.setAffirmStatus(2);
          int id=workOrderMapper.addWorkOrder(workOrder);
          //推送
          List<String> clientIdList = pushMapper.getClientId(orderDelivery.getProjectId());
          PushUtils.sendMessage(clientIdList,orderDelivery.getRoomName(),orderDelivery.getDeviceName(),orderDelivery.getFault());
          //自动派单
/*          Map<Double, String> distanceMap = new HashMap<>();
          List<Double> distanceList = new ArrayList<>();*/
          //Project project = projectMapper.queryProjectByProId(projects.getProjectId());
          //if (WebSocketServiceImpl.locationMap.size() > 0) {
/*            for (Map.Entry<String, String> entry : WebSocketServiceImpl.locationMap.entrySet()) {
              Double distance = CalculateDistanceUtils.getDistance(entry.getValue(), project.getLongitudeLatitude() + "," + project.getLatitude_latitude(), Ellipsoid.Sphere);
              distanceMap.put(distance, entry.getKey());
              distanceList.add(distance);
            }*/
            //Collections.sort(distanceList);
            //for (Double distance : distanceList) {
              //String maintainerId = distanceMap.get(distance);
              //List<WorkOrder> orderList = workOrderMapper.getBeingProcessedOrderByMaintainerId(null, null, maintainerId);
              //if (orderList.size() > 0) {
              //  if (orderDelivery.getProjectId().equals(orderList.get(0).getProjectId())) {
                  for (Marketing marketing : marketings) {
                    if(marketing.getStatus() == 1){
                      Maintainer maintainer = maintainerMapper.selectById1(marketing.getMaintainerId());
                      workOrder.setMaintainerId(Integer.parseInt(maintainer.getMaintainerId()));
                      workOrder.setMaintainerName(maintainer.getName());
                      workOrder.setPhone(maintainer.getPhone());
                      workOrder.setDealBeginTime(new Date());
                      workOrder.setOrderDeliveryTime(new Date());
                      workOrder.setOrderStatus(2);
                      workOrderMapper.updateWorkOrderById(workOrder);
                    }
                    //发送短信
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("projectName", workOrder.getProjectName());
                    map.put("powerRoomName", workOrder.getRoomName());
                    map.put("deviceName", workOrder.getDeviceName());
                    map.put("fault", workOrder.getFault());
                    String data = JSONObject.toJSONString(map);
                    SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                  }
                  orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                 // break;
               /* } else {
                  continue;
                }*/
             /* } else {
                Maintainer maintainer = maintainerMapper.selectById1(maintainerId);
                //创建工单
                workOrder.setMaintainerId(Integer.valueOf(maintainer.getMaintainerId()));
                workOrder.setMaintainerName(maintainer.getName());
                workOrder.setPhone(maintainer.getPhone());
                workOrder.setDealBeginTime(new Date());
                workOrder.setOrderDeliveryTime(new Date());
                workOrder.setOrderStatus(2);
                for (Marketing marketing : marketings) {
                  //发送短信
                  Map<String, String> map = new HashMap<String, String>();
                  map.put("projectName", workOrder.getProjectName());
                  map.put("powerRoomName", workOrder.getRoomName());
                  map.put("deviceName", workOrder.getDeviceName());
                  map.put("fault", workOrder.getFault());
                  String data = JSONObject.toJSONString(map);
                  SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                }
                workOrderMapper.updateWorkOrderById(workOrder);
                orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                break;
              }*/
            //}
          /*}else {
            List<Maintainer> MaintainerList = maintainerMapper.getAll();
            int flag=0;
            for (Maintainer maintainer:MaintainerList){
              if(maintainer.getMaintainerId()!=null){
                WorkOrder workOrderByMaintainerId = workOrderMapper.getWorkOrderByMaintainerId(maintainer.getMaintainerId());
                if(workOrderByMaintainerId==null){
                  workOrder.setMaintainerId(Integer.valueOf(maintainer.getMaintainerId()));
                  workOrder.setMaintainerName(maintainer.getName());
                  workOrder.setPhone(maintainer.getPhone());
                  workOrder.setDealBeginTime(new Date());
                  workOrder.setOrderDeliveryTime(new Date());
                  workOrder.setOrderStatus(2);
                  workOrderMapper.updateWorkOrderById(workOrder);
                  orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
                  for (Marketing marketing : marketings) {
                    //发送短信
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("projectName", workOrder.getProjectName());
                    map.put("powerRoomName", workOrder.getRoomName());
                    map.put("deviceName", workOrder.getDeviceName());
                    map.put("fault", workOrder.getFault());
                    String data = JSONObject.toJSONString(map);
                    SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
                  }
                  flag=1;
                  break;
                }
              }
            }
            if (flag==0){
              workOrder.setMaintainerId(15858206);
              workOrder.setMaintainerName("田俊齐");
              workOrder.setPhone("18749493600");
              workOrder.setDealBeginTime(new Date());
              workOrder.setOrderDeliveryTime(new Date());
              workOrder.setOrderStatus(2);
              workOrderMapper.updateWorkOrderById(workOrder);
              orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrder.getOrderDeliveryId()));
              for (Marketing marketing : marketings) {
                //发送短信
                Map<String, String> map = new HashMap<String, String>();
                map.put("projectName", workOrder.getProjectName());
                map.put("powerRoomName", workOrder.getRoomName());
                map.put("deviceName", workOrder.getDeviceName());
                map.put("fault", workOrder.getFault());
                String data = JSONObject.toJSONString(map);
                SmsUtils.sendSms(marketing.getPhone(),"合一电力","SMS_196616561",data);
              }
            }


          }*/

          return insert;
        }
        }
      }



    return 0;
  }
}
